//var Keyboard = require('./keyboard.js'),
//    fs = require('fs');
//var k = new Keyboard('event4');
//var write = function(obj){
//    fs.appendFile('./kb.log',obj.keyId,'utf8',function(err){
//        fs.writeFile('./error.log',err)
//    });
//
//}
//k.on('keyup', write);
//k.on('keydown', write);
////k.on('keypress', console.log);
//
//k.on('error', console.error); // Something wrent wrong, keyboard disconnected or something
//

var EV_KEY = 1,
    EVENT_TYPES = ['keyup', 'keypress', 'keydown'];
var fs = require('fs');
var Kb = {
    init: function () {
        Kb.bufferSize = 24,
            Kb.buf = new Buffer(this.bufferSize)
    },

    onOpen: function (err, fd) {

        if (err) {
            console.error(err);
            return;
        }
        Kb.fd = fd;
        Kb.startRead();
    },
    startRead: function () {
        fs.read(Kb.fd, Kb.buf, 0, Kb.bufferSize, null, Kb.onRead);
    },
    onRead: function (bytesRead) {
        var event = parse(this, this.buf);
    }

}
Kb.init();
function parse(input, buffer) {

    console.info(input);

    var event, value;

//    if( buffer.readUInt16LE(16) === EV_KEY ) {
//
//        event = {
//            timeS: buffer.readUInt64LE(0),
//            timeMS: buffer.readUInt64LE(8),
//            keyCode: buffer.readUInt16LE(18)
//        };
//
//        event.keyId = findKeyID(event.keyCode);
//        event.type = EVENT_TYPES[ buffer.readUInt32LE(20) ];
//
//    }

    return event;
};


fs.open('/dev/input/event4', 'r', Kb.onOpen, function (err) {
    if (err) {
        console.error(err);
    }
    console.info("done");
});